/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model;

import java.util.List;

import com.lineage.configure.Config;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.packetserver.S_TradeAddItem;
import com.lineage.server.packetserver.S_TradeStatus;
import com.lineage.server.utils.LogRecorder;

// Referenced classes of package com.lineage.server.model:
// L1Trade

/**
 * 交易
 */
public class L1Trade {

    private static L1Trade _instance;

    public static L1Trade getInstance() {
        if (_instance == null) {
            _instance = new L1Trade();
        }
        return _instance;
    }

    public L1Trade() {
    }

    /**
     * 增加交易物品.
     * 
     * @param player
     *            对象
     * @param itemid
     *            道具编号
     * @param itemcount
     *            道具数量
     */
    public final void tradeAddItem(final L1PcInstance player, final int itemid,
            int itemcount) {
        final L1PcInstance trading_partner = (L1PcInstance) L1World
                .getInstance().findObject(player.getTradeID());
        final L1ItemInstance l1iteminstance = player.getInventory().getItem(
                itemid);
        if (l1iteminstance != null) {
            itemcount = Math.abs(itemcount); // 确保物品数量为正
            itemcount = Math.min(itemcount, l1iteminstance.getCount()); // 确保交易的物品小于身上所有
            if (trading_partner != null) {
                if (!l1iteminstance.isEquipped()) {
                    if ((itemcount < 0) || (itemcount > 2000000000)
                            || (l1iteminstance.getCount() < 0)
                            || (l1iteminstance.getCount() < itemcount)) {
                        return;
                    }
                    if ((l1iteminstance.getCount() < itemcount)
                            || (0 > itemcount)) {
                        player.sendPackets(new S_TradeStatus(1));
                        trading_partner.sendPackets(new S_TradeStatus(1));
                        player.setTradeOk(false);
                        trading_partner.setTradeOk(false);
                        player.setTradeID(0);
                        trading_partner.setTradeID(0);
                        return;
                    }
                    player.getInventory().tradeItem(l1iteminstance, itemcount,
                            player.getTradeWindowInventory());
                    player.sendPackets(new S_TradeAddItem(l1iteminstance,
                            itemcount, 0));
                    trading_partner.sendPackets(new S_TradeAddItem(
                            l1iteminstance, itemcount, 1));
                }
            }
        }
    }

    /**
     * 交易取消.
     * 
     * @param player
     *            对象
     */
    public final void tradeCancel(final L1PcInstance player) {
        int cnt;
        final L1PcInstance trading_partner = (L1PcInstance) L1World
                .getInstance().findObject(player.getTradeID());
        if (trading_partner != null) {
            final List<L1ItemInstance> player_tradelist = player
                    .getTradeWindowInventory().getItems();
            final int player_tradecount = player.getTradeWindowInventory()
                    .getSize();

            final List<L1ItemInstance> trading_partner_tradelist = trading_partner
                    .getTradeWindowInventory().getItems();
            final int trading_partner_tradecount = trading_partner
                    .getTradeWindowInventory().getSize();

            for (cnt = 0; cnt < player_tradecount; cnt++) {
                final L1ItemInstance l1iteminstance1 = player_tradelist.get(0);
                player.getTradeWindowInventory().tradeItem(l1iteminstance1,
                        l1iteminstance1.getCount(), player.getInventory());
            }
            for (cnt = 0; cnt < trading_partner_tradecount; cnt++) {
                final L1ItemInstance l1iteminstance2 = trading_partner_tradelist
                        .get(0);
                trading_partner.getTradeWindowInventory().tradeItem(
                        l1iteminstance2, l1iteminstance2.getCount(),
                        trading_partner.getInventory());
            }

            player.sendPackets(new S_TradeStatus(1));
            trading_partner.sendPackets(new S_TradeStatus(1));
            player.setTradeOk(false);
            trading_partner.setTradeOk(false);
            player.setTradeID(0);
            trading_partner.setTradeID(0);
        }
    }

    /**
     * 交易完成.
     * 
     * @param player
     *            对象
     */
    public final void tradeOK(final L1PcInstance player) {
        int cnt;
        final L1PcInstance trading_partner = (L1PcInstance) L1World
                .getInstance().findObject(player.getTradeID());
        if (trading_partner != null) {
            final List<L1ItemInstance> player_tradelist = player
                    .getTradeWindowInventory().getItems();
            final int player_tradecount = player.getTradeWindowInventory()
                    .getSize();

            final List<L1ItemInstance> trading_partner_tradelist = trading_partner
                    .getTradeWindowInventory().getItems();
            final int trading_partner_tradecount = trading_partner
                    .getTradeWindowInventory().getSize();

            for (cnt = 0; cnt < player_tradecount; cnt++) {
                final L1ItemInstance l1iteminstance1 = player_tradelist.get(0);
                player.getTradeWindowInventory().tradeItem(l1iteminstance1,
                        l1iteminstance1.getCount(),
                        trading_partner.getInventory());
                // 交易纪录
                if (Config.writeTradeLog) {
                    LogRecorder.writeTradeLog(player, trading_partner,
                            l1iteminstance1);
                }
            }
            for (cnt = 0; cnt < trading_partner_tradecount; cnt++) {
                final L1ItemInstance l1iteminstance2 = trading_partner_tradelist
                        .get(0);
                trading_partner.getTradeWindowInventory().tradeItem(
                        l1iteminstance2, l1iteminstance2.getCount(),
                        player.getInventory());
            }

            player.sendPackets(new S_TradeStatus(0));
            trading_partner.sendPackets(new S_TradeStatus(0));
            player.setTradeOk(false);
            trading_partner.setTradeOk(false);
            player.setTradeID(0);
            trading_partner.setTradeID(0);
            player.turnOnOffLight();
            trading_partner.turnOnOffLight();
        }
    }
}
